/*7-3 树的同构 (25 分)*/
#include<iostream>
using namespace std;
#define MaxTree 10
#define Elemtype char
#define Tree int
#define Null -1

struct TreeNode{
    Elemtype Element;
    Tree Left;
    Tree Right;
}T1[MaxTree],T2[MaxTree];

int Issomorphic(Tree R1, Tree R2)
{
	if (R1 == Null && R2 == Null) return 1;//均为空树
	if ((R1 != Null && R2 == Null)||(R1 == Null && R2 != Null)) return 0;//两者中的其一为空树
	if (T1[R1].Element != T2[R2].Element) return 0;//根节点的数据不同
	if (T1[R1].Left == Null && T2[R2].Left == Null) return Issomorphic(T1[R1].Right, T2[R2].Right);//左子树都为空，递归判断右子树是否同构
	if (T1[R1].Right == Null && T2[R2].Right == Null) return Issomorphic(T1[R1].Left, T2[R2].Left);//右子树都为空，判断左子树是否同构
	if (T1[R1].Left != Null && T2[R2].Left != Null && (T1[T1[R1].Left].Element == T2[T2[R2].Left].Element))
		return(Issomorphic(T1[R1].Left, T2[R2].Left)) && (Issomorphic(T1[R1].Right, T2[R2].Right));//左子树不为空，且根节点相同，递归判断左子树和右子树是否同构
	else return (Issomorphic(T1[R1].Left, T2[R2].Right)) && (Issomorphic(T1[R1].Right, T2[R2].Left));//如果两棵树左子树（一个空一个不空或者都不空）并且数据不一样，那么判断第一棵树的左（右）子树是否跟第二棵树的右（左）子树同构，左右调换 */
}

Tree bulidtree(struct TreeNode T[],int N){
    int check[MaxTree] = {0},root = Null;
    if(N){
        for(int i=0;i<N;i++){
            Elemtype c1,c2;
            cin >>T[i].Element>>c1>>c2;
            if(c1!='-'){
                T[i].Left = c1-'0';
                check[T[i].Left] = 1;
            }else{
                T[i].Left = Null;
            }
            if(c2!='-'){
                T[i].Right = c2 -'0';
                check[T[i].Right] = 1;
            }else{
                T[i].Right = Null;
            }
            for(int i=0;i<N;i++){
                if(check[i]==0){
                    root = i;
                    break;
                }
            }
        }
    }
    return root;
}
int main(){
    int N1;
    cin>>N1;
    Tree R1,R2;
    R1 = bulidtree(T1,N1);

    int N2;
    cin>>N2;
    R2 = bulidtree(T2,N2);

    if(Issomorphic(R1,R2)){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}